package com.lime49.lockcrypt;
import java.util.*;
import javax.swing.tree.*;
import java.text.SimpleDateFormat;

public class AccountTreeNode extends DefaultMutableTreeNode {
    private String name, parentName;
    private boolean isDeleted = false, isChanged = false;
    private Date created, modified;
    
    public AccountTreeNode(String name, String parentName) {
        this.name = name;
        this.parentName = parentName;
        this.created = new Date();
        this.modified = new Date();
    }
    public AccountTreeNode(String name, String parentName, boolean isChanged) {
        this(name, parentName);
        this.isChanged = isChanged;
    }
    public AccountTreeNode(String name, String parentName, boolean isChanged, long created, long modified) {
        this(name, parentName, isChanged);
        this.created = new Date(created);
        this.modified = new Date(modified);
    }
    
    /**
     * Returns the name of this node
     * @return The name of this node
     */
    public String getName() {
        return name;
    }
    
    /**
     * Sets the name of this node
     * @param name The new name for this node
     */
    public void setName(String name) {
        this.name = name;
        isChanged = true;
    }
    
    /**
     * Returns the parent name of this node
     * @return The parent name of this node
     */
    public String getParentName() {
        return parentName;
    }
    
    /**
     * Sets the parent name of this node
     * @param name The new parent name for this node
     */
    public void setParentName(String name) {
        parentName = name;
        isChanged = true;
    }
    
    /**
     * Returns whether or not this node has been marked as deleted
     * @return True if the account has been deleted, otherwise false
     */
    public boolean getIsDeleted() {
        return isDeleted;
    }
    
    /**
     * Sets the isChanged and isDeleted flags to true to prevent this node from being re-synchronised to the database
     */
    public void delete() {
        isDeleted = true;
        isChanged = true;
    }
    
    /**
     * Returns whether or not this node has changed since the database was last loaded
     * @return True if the node has changed, otherwise false
     */
    public boolean getIsChanged() {
        return this.isChanged;
    }
    
    /**
     * Sets the isChanged flag for this node and updates it's last modified time
     * @param isChanged
     */
    public void setIsChanged(boolean isChanged) {
        this.isChanged = isChanged;
        this.modified = new Date();
    }
    
    /**
     * Returns the date this node was created
     * @return The date this node was created
     */
    public long getCreated() {
        return created.getTime();
    }
    
    /**
     * Returns the date this node was last modified
     * @return The date this node was last modified
     */
    public long getModified() {
        return modified.getTime();
    }
    
    /**
     * Sets the date this node was last modified to the current date
     */
    public void setModified() {
        this.modified = new Date();
    }
    
    /**
     * Returns the date this node was created
     * @return The date this node was created
     */
    public Date getDateCreated() {
        return created;
    }
    
    /**
     * Returns the date this node was last modified
     * @return The date this node was last modified
     */
    public Date getDateModified() {
        return modified;
    }
    
    /**
     * Returns the date this account was created, formatted using the specified SimpleDateFormat
     * @param format The format to use
     * @return The formatted creation date
     */
    public String getDateCreatedPretty(SimpleDateFormat format) {
        return format.format(created);
    }
    
    /**
     * Returns the date this account was last modified, formatted using the specified SimpleDateFormat
     * @param format The format to use
     * @return The formatted last modified date
     */
    public String getDateModifiedPretty(SimpleDateFormat format) {
        return format.format(modified);
    }
}
